/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
/*
  // 层序遍历，当左右节点都为空的时返回深度
    int minDepth(TreeNode* root) {
        queue<TreeNode*> q;
        if(nullptr != root) q.push(root);
        int depth = 0;
        while(!q.empty()){
            size_t size = q.size();
            ++depth;
            for(int i = 0; i < size; ++i){
                TreeNode* node = q.front();
                q.pop();
                if(node->left != nullptr) q.push(node->left);
                if(node->right != nullptr) q.push(node->right);
                if(node->left == nullptr && node->right == nullptr)
                    return depth;
            }
        }
        return depth;
    }
*/
    int minDepth(TreeNode* root) {
        if(nullptr == root) return 0;
        if(root->left == nullptr && root->right == nullptr)
            return 1;
        int _min = INT_MAX;
        if(root->left != nullptr)
            _min = min(minDepth(root->left), _min);
        if(root->right != nullptr)
            _min = min(minDepth(root->right), _min);
        return _min+1;
    }
};